/*****************************************************************************
Description:

This program generates the running variable controls

*	----------------------------------------------------------------------------
*	inputs: 		"${clean_code_working}program_pscore`modification_str'`suffix'_`year'`grad_flag'.dta"
*						> P-score file from pscores_mdrd2
*
*	----------------------------------------------------------------------------
*	outputs: 		"${clean_code_working}runvar_dataset_program_pscore`modification_str'`suffix'_`year'`grad_flag'.dta"
*						> Data set with running variable controls
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

* Settings
set trace off
set tracedepth 1

* Arguments
local infile `1'

use "${cleandata}/`infile'", clear

* Only compute for programs that have a bandwidth
keep if has_bw == 1

/*
The rv controls are going to be
1- applying to the program
2- Being in the bandwidth
3- the RV in the bandwidth
4- the Rv in the bandwidth + being above the cutoff
*/
// RV Control 1
gen byte `grad_flag'rv_app_  = 1

// RV Control 2
gen byte `grad_flag'rv_in_bw_ = (t_c == 1) & (rv_app_ == 1)

// RV Control 3
gen `grad_flag'rv_ =  hs_rank_centered * rv_in_bw_

// RV Control 4
gen `grad_flag'rv_above_ =   ( rv_ > 0 ) * hs_rank_centered * rv_in_bw_

// Alternative versions that currently not used

// Square the running variable
gen `grad_flag'quad_ 			=  hs_rank_centered^2 * rv_in_bw_
gen `grad_flag'quad_above_ 		=  ( rv_ > 0 ) * hs_rank_centered^2 * rv_in_bw_

// Keep relevant variables
keep stu `grad_flag'rv_* prg_bkt `grad_flag'quad_*

// Reshape to make unique by applicants
reshape wide `grad_flag'rv_in_bw_ `grad_flag'rv_ `grad_flag'rv_above_ `grad_flag'rv_app_ `grad_flag'quad_ `grad_flag'quad_above_, i(stu) j(prg_bkt) string

keep stu rv_* quad_*
duplicates drop
isid stu

compress
sa "${cleandata}runvar_dataset_`infile'", replace
